version 13      
set more off 
clear all

use "2_survey_data.dta", clear

order ResponseID consent citizenship state county not_aff_m ///
not_aff_h roads_m roads_h school_m school_h comm_m comm_h ///
food_m food_h evacuation_m evacuation_h finloss_m finloss_h ///
injury_m injury_h avalanche coast_hazards drought earthquake ///
 flooding fog hail heat hurricane landslide lightning storms /// 
 tornado tsunami volcano wildfire wind wint_weather other ///
 matthew aff_norm need_norm pol_ties gvt_prio dam_support ///
 dam_support1 yourself family local_respond state_gvt ///
 fed_agenciesf vol_orgas affchild survRiskPref1 survRiskPref ///
 survTimePref1 survTimePref donate charities donation_1 partyyn ///
 partyID partyID_TEXT leftright trust1 trust2 knowledge sex ///
 race education relationship childnumb occOwn income highInc ///
 home quality quality_text uniqueID age LocationLatitude ///
 LocationLongitude LocationAccuracy sum_afftype afftype_miss ///
 affected_m affected_mh affected_m_or_h affected_h fin_cost ///
 framing rel_aid prep spen_diff dam_cost timepref_surv riskpref_surv ///
 race1 income_inter trust right_answer uni_degree educ_groups ///
 married children occupation missing_af color_correct ///
 seq_timepref_surv timepref_surv_high seq_riskpref_surv ///
 riskpref_surv_high seq_respdis respdis_high seq_age age_high ///
 seq_trust trust_high seq_env_income env_income_high seq_income ///
 income_high seq_aff_norm aff_norm_high seq_need_norm need_norm_high ///
 seq_pol_ties pol_ties_high seq_gvt_prio gvt_prio_high seq_riskdest ///
 riskdest_high seq_effprep effprep_high id

 
 
********************************************************************************
/* conjoint - reshape */
********************************************************************************
*load data and reshape to scenario level data							 
*keep  uniqueID cj1_* cj2_* cj3_* cj4_*
order uniqueID

*reshape to scenarios
reshape long cj1_FeatHHInc cj1_FeatDamage cj1_FeatDead cj1_FeatPresVote cj1_FeatUnemp cj1_FeatPresident cj1_relief ///
             cj2_FeatHHInc cj2_FeatDamage cj2_FeatDead cj2_FeatPresVote cj2_FeatUnemp cj2_FeatPresident cj2_relief ///
			 cj3_FeatHHInc cj3_FeatDamage cj3_FeatDead cj3_FeatPresVote cj3_FeatUnemp cj3_FeatPresident cj3_relief ///
             cj4_FeatHHInc cj4_FeatDamage cj4_FeatDead cj4_FeatPresVote cj4_FeatUnemp cj4_FeatPresident cj4_relief ///
			 ,i(uniqueID) j(scenario)						 

forvalues i = 1/4 {
renvars cj`i'_* , postfix(_`i')
renvars cj`i'_* , predrop(4)
}			 
order scenario

reshape long FeatHHInc_ FeatDamage_ FeatDead_ FeatPresVote_ FeatUnemp_ FeatPresident_ relief_, i(uniqueID scenario)	

rename _j conjointno
order uniqueID conjointno scenario 
sort  uniqueID conjointno scenario 
egen contestid = group(uniqueID conjointno)

*create categorical variables
egen FeatHHInc = cut(FeatHHInc_), at(10000,40000,70000,100000,100001) label
egen FeatDamage = cut(FeatDamage_), at(0.1,1,4,24,25) label
egen FeatDead = cut(FeatDead_), at(0,5,6) label
egen FeatUnemp = cut(FeatUnemp_), at(3,5,7,9,10) label
rename FeatPresVote_ FeatPresVote

gen log_relief = log(relief_+1)

drop FeatHHInc_ FeatDead_ FeatUnemp_ 
rename FeatDamage_ damagevalue
egen test_con = sum(relief_), by(uniqueID conjointno)
tab test_con

gen inc10 = (FeatHHInc==0)
gen inc40 = (FeatHHInc==1)
gen inc70 = (FeatHHInc==2)
gen inc100 = (FeatHHInc==3)

tab FeatDamage, gen(damage_cj)
rename damage_cj1 damage_100k 
rename damage_cj2 damage_1m
rename damage_cj3 damage_4m
rename damage_cj4 damage_24m

* Generate variable that counts the total amount of damage per conjoint task
order uniqueID conjointno scenario

* Aux value var
egen aux_id=group(uniqueID conjointno)
order aux_id

* Loop over aux id and compute total damage per conjoint
gen damagesum=.
su aux_id
forvalues i=1(1)`r(max)' {
su damagevalue if aux_id==`i'
replace damagesum=`r(sum)' if aux_id==`i'
}
tab damagesum
gen totaldamageunder10m=.
replace totaldamageunder10m=0 if damagesum>10
replace totaldamageunder10m=1 if damagesum<=10
tab totaldamageunder10m damagesum

tab FeatDead, gen(fatal_cj)
rename fatal_cj1 fatal_none
rename fatal_cj2 fatal_5

tab FeatUnemp, gen(unemp_cj)
rename unemp_cj1 unemp_3
rename unemp_cj2 unemp_5
rename unemp_cj3 unemp_7
rename unemp_cj4 unemp_9

tab FeatPresVote, gen(presvote_cj)
rename presvote_cj1 presvote_50D
rename presvote_cj2 presvote_40D
rename presvote_cj3 presvote_30D
rename presvote_cj4 presvote_60D
rename presvote_cj5 presvote_70D

* Interactions
foreach var in inc10 inc40 inc70 inc100 {
gen `var'Xdamage_24m=`var'*damage_24m
gen `var'Xdamage_4m=`var'*damage_4m
gen `var'Xdamage_1m=`var'*damage_1m
}

*save pilot_conjoint2.dta, replace


/* ebal */
* Age (same categories we used as target for the sample)
gen agegr18_24 = (age > 17 & age <25)
gen agegr25_44 = (age > 24 & age < 45)
gen agegr45_64 = (age > 44 & age < 65)
gen agegr65_o = (age > 64 & age < 150)

* Edcuation (same categories we used as target for the sample)
gen lessThanHighSchool = (education > 0 & education < 4)
gen highSchoolDegree = education == 4
gen someCollege = (education > 4 & education < 7)
gen bachelor = education == 7
gen advancedDegree = (education > 7 & education < 11)


foreach var of varlist agegr18_24-agegr65_o lessThanHighSchool-advancedDegree {
tab `var'
}

* Now we run the ebalance command to reweight the sample
* As target we use the data from the file Dropbox/Redistribution behavior/09 Quotas/Survey US_Quoten_Respondi_2760CompletedResponses.xls
ebalance agegr18_24 agegr25_44 agegr45_64 sex lessThanHighSchool highSchoolDegree someCollege bachelor, ///
manualtargets(.126 .342 .339 .487 .122 .296 .284 .192)

* Rename weighting variable
rename _webal weight
gen relief_rob = relief_/10
save "conjoint_scenariolevel.dta", replace

exit




